package com.zerro.litez.compiler.processor.parser;

import com.zerro.litez.annotations.dao.Query;

import java.lang.annotation.Annotation;
import java.util.*;

/**
 * 注解信息，通过将注解转换为字符串的形式，解决不能获取class类型的属性的问题
 */
public class AnnotationInfo {

    private final String strAnno;
    private final String name;
    private final Map<String, String> valuesMap = new HashMap<>();

    public AnnotationInfo(Annotation anno) {
        this(anno.toString());
    }

    public AnnotationInfo(String strAnno) {
        this.strAnno = strAnno;
        String[] fullName = strAnno.split("\\(")[0].split("\\.");
        name = fullName[fullName.length - 1];

        if (name.equals(Query.class.getSimpleName())) {
            parseQueryValue();
        } else {
            parseValue();
        }
    }

    private void parseValue() {
        String allValue = strAnno.split("\\(")[1];

        List<String> valuesTemp = Arrays.asList(
                allValue.substring(0, allValue.length() - 1).split("="));

        List<String> keys = new ArrayList<>();
        List<String> values = new ArrayList<>();

        String first = valuesTemp.get(0).replaceAll("[{}\"]", "");
        if (valuesTemp.size() == 1){
            valuesMap.put("value", first);
        } else {
            keys.add(first);
            for (int i = 1; i < valuesTemp.size() - 1; i++) {
                String str = valuesTemp.get(i);
                int sp = str.lastIndexOf(", ");
                values.add(str.substring(0, sp).replaceAll("[{}\"]", ""));
                keys.add(str.substring(sp + 2).replaceAll("[{}\"]", ""));
            }
            values.add(valuesTemp.get(valuesTemp.size() - 1).replaceAll("[{}\"]", ""));

            for (int i = 0; i < keys.size(); i++) {
                valuesMap.put(keys.get(i), values.get(i));
            }
        }
    }

    private void parseQueryValue() {
        String valueStr = strAnno.split("\\(")[1];
        String[] arr = valueStr.substring(0, valueStr.length() - 1).split("=\"");
        String value = "";
        if (arr.length > 1){
            value = arr[1].substring(0, arr[1].length() - 1);
        }
        valuesMap.put(arr[0], value);

    }

    public String getValue(String name) {
        return valuesMap.get(name);
    }

    public String getValue() {
        return valuesMap.get("value");
    }

    public static <T extends Annotation> String getValueOf(T annotation, String name) {
        String strAnno = annotation.toString();
        String allValues = strAnno.split("\\(")[1];
        int index = allValues.indexOf(name) + name.length() + 1;

        String value = allValues.substring(index).split(",", 2)[0];

        if (value.endsWith(")")) {
            value = value.substring(0, value.length() - 1);
        }
        return value;
    }

    public Collection<String> getValues() {
        return valuesMap.values();
    }

    public String getName() {
        return name;
    }

    @Override
    public String toString() {
        return this.getClass().getSimpleName() + "{"
                + name + ", "
                + valuesMap
                + "}";
    }


}
